package com.example.myandroidapp1;

import java.io.InputStream;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class MyGallery extends Activity implements OnClickListener {

	private ImageView image;
	private Button button;
	private ProgressBar progressBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery);
		initialize();
		new ImageLoader().execute(new String[] {});
		button.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bChangeImage:
			new ImageLoader().execute(new String[] {});
			break;

		default:
			break;
		}

	}

	private void initialize() {
		image = (ImageView) findViewById(R.id.ivPhoto);
		button = (Button) findViewById(R.id.bChangeImage);
		progressBar = (ProgressBar) findViewById(R.id.pbForImage);
	}

	private class ImageLoader extends AsyncTask<String[], Integer, Bitmap> {

		
		@Override
		protected Bitmap doInBackground(String[]... urls) {
			String image_url = "http://166.78.125.197/mysite/web/var/ezflow_site/storage/images/gallery-1/dsc_0211/478-1-eng-US/DSC_0211_gallerythumbnail.jpg";//"http://api.androidhive.info/images/sample.jpg";
			try {
				Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
						image_url).getContent());
				return bitmap;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(Bitmap result) {
			progressBar.setVisibility(View.INVISIBLE);
			image.setImageBitmap(result);
			
		}

	}
}
